package com.compass.doctor.ui.home

import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.view.View
import androidx.activity.viewModels
import androidx.fragment.app.Fragment
import com.compass.doctor.R
import com.compass.doctor.databinding.ActivityInquiryOrderBinding
import com.compass.doctor.ui.home.fragment.InquiryOrderFragment
import com.compass.doctor.ui.home.viewmodel.InquiryOrderViewModel
import com.compass.framework.adapter.ViewPage2FragmentAdapter
import com.compass.doctor.base.BaseVBActivity
import com.compass.framework.constant.CHAT_INFO
import com.compass.framework.constant.KEY_INDEX
import com.compass.framework.constant.KEY_ORDER_TYPE
import com.compass.framework.constant.REFRESH_MESSAGE
import com.compass.framework.constant.SET_INDEX
import com.compass.framework.ext.gone
import com.compass.framework.ext.onClick
import com.compass.framework.ext.visible
import com.compass.framework.utils.LiveDataBus

/**
 * Description： 问诊单界面
 * Date:2023/12/12 9:09
 */
class InquiryOrderActivity : BaseVBActivity<ActivityInquiryOrderBinding>() {


    companion object {
        fun start(context: Context, index: Int) {
            val intent = Intent(context, InquiryOrderActivity::class.java)
            intent.putExtra(KEY_INDEX, index)
            context.startActivity(intent)
        }
    }

    private val inquiryOrderViewModel: InquiryOrderViewModel by viewModels()
    private lateinit var viewPageAdapter: ViewPage2FragmentAdapter
    private var fragments: ArrayList<Fragment> = ArrayList()

//    private lateinit var onReceptionFragment: InquiryOrderFragment
    private lateinit var alreadyFragment: InquiryOrderFragment
    private lateinit var completeFragment: InquiryOrderFragment
    private var index:Int=0
    override fun initView(savedInstanceState: Bundle?) {
        index = intent.getIntExtra(KEY_INDEX,0)
        initFragment()
        mBinding.imgClose.onClick {
            mBinding.llSettingTips.visibility = View.GONE
        }
        mBinding.dslTab.tabIndicator.indicatorWidth = -2
        mBinding.dslTab.tabIndicator.indicatorEnableFlow = true
        mBinding.dslTab.configTabLayoutConfig {
            onSelectIndexChange = { _, selectIndexList, _, _ ->
                val toIndex = selectIndexList.first()
                mBinding.viewpager2.setCurrentItem(toIndex, false)
            }
        }
        viewPageAdapter = ViewPage2FragmentAdapter(supportFragmentManager, lifecycle, fragments)
        mBinding.viewpager2.adapter = viewPageAdapter
        mBinding.viewpager2.isUserInputEnabled = false//禁止viewPage滑动
//        mBinding.viewpager2.setCurrentItem(index, false)
        mBinding.dslTab.setCurrentItem(index)

    }


    override fun initData() {
//        inquiryOrderViewModel.getConsultationOrderCount()
//        inquiryOrderViewModel.orderCountLiveData.observe(this) {
//            it?.let {number->
//                if (number>0){
//                    mBinding.tvTips.text = String.format(getString(R.string.clinical_reception_tips), number)
//                    mBinding.llSettingTips.visible()
//                }else{
//                    mBinding.llSettingTips.gone()
//                }
//
//            }?:kotlin.run {
//                mBinding.llSettingTips.gone()
//            }
//
//        }

        LiveDataBus.get().with(SET_INDEX, String::class.java)
            .observe(this) {
                mBinding.dslTab.setCurrentItem(1)
            }

    }

    private fun initFragment() {
//        onReceptionFragment = InquiryOrderFragment()
//        val bundleReception = Bundle()
//        bundleReception.putInt(KEY_ORDER_TYPE, 1)
//        onReceptionFragment.arguments = bundleReception
//        fragments.add(onReceptionFragment)

        alreadyFragment = InquiryOrderFragment()
        val bundleAlready = Bundle()
        bundleAlready.putInt(KEY_ORDER_TYPE, 2)
        alreadyFragment.arguments = bundleAlready
        fragments.add(alreadyFragment)

        completeFragment = InquiryOrderFragment()
        val bundleComplete = Bundle()
        bundleComplete.putInt(KEY_ORDER_TYPE, 3)
        completeFragment.arguments = bundleComplete
        fragments.add(completeFragment)


    }
}